import {request, delay, formatDate} from "../../utils/request"

const initDateRange = () => {
    const date = new Date()
    const endDate = formatDate(date)
    const startDate = date.setDate(date.getDate() - 10)
    return {
        endDate,
        startDate: formatDate(new Date(startDate))
    }
}
const {startDate, endDate} = initDateRange()
Page({
    data: {
        active_id: wx.getStorageSync("active_id"),
        userInfo: wx.getStorageSync("userinfo"),
        ding_dan: {},
        shou_ru: {},
        historyOrder: {},
        historyMoney: {},
        startDate1: startDate,
        endDate1: endDate,
        startDate2: startDate,
        endDate2: endDate,
        token: wx.getStorageSync("token"),
    },
    setHistoryData(data, type) {
        if (type === 0 || type === 1) {
            const historyOrder = {
                chashi_order_num_wx: "",
                chashi_order_num_yue: "",
                chashi_order_num_meituan: "",
                chashi_order_num_douyin: "",
                shipin_order_num: "",
                xudan_order_num: "",
                chongzhi_num: "",
                chayishi_num: "",
            }
            Object.keys(historyOrder).forEach(key => {
                const value = data[key]
                historyOrder[key] = value || "0.00"
            })
            this.setData({
                historyOrder
            })
        }
        if (type === 0 || type === 2) {
            const historyMoney = {
                chashi_order_price_weixin: "",
                chashi_order_price_yue: "",
                chashi_order_price_meituan: "",
                chashi_order_price_douyin: "",
                shipin_order_price: "",
                xudan_order_price: "",
                chongzhi_price: "",
                chayishi_price: "",
                xudan_order_price_wx: "",
                xudan_order_price_yue: "",
                chashi_order_price: "",
                weixin_pay_price: "",
            }
            Object.keys(historyMoney).forEach(key => {
                const value = data[key]
                historyMoney[key] = value || "0.00"
            })
            this.setData({
                historyMoney
            })
        }
    },
    async initHistoryData(type = 0) {
        wx.showLoading({
            title:"正在获取"
        })
        const {mobile} = this.data.userInfo
        const {active_id} = this.data
        const {startDate1, endDate1, startDate2, endDate2} = this.data
        let start_date = startDate1, end_date = endDate1
        if (type === 2) {
            start_date = startDate2
            end_date = endDate2
        }
        const resp = await request({
            url: "/shujutongji/lishitongji",
            data: {
                store_id: active_id,
                mobile,
                // date: "2024-05-01",
                start_date,
                end_date
            }
        }).catch(()=>wx.hideLoading())
        const {code, data} = resp
        await delay(250)
        wx.hideLoading()
        if (code !== 1) return
        this.setHistoryData(data, type)
    },

    handleStartDateChange2(ev) {
        const {value} = ev.detail
        const startDate = new Date(value.replace(/-/g, '/'))
        const endDate = new Date(this.data.endDate2)
        if (startDate.getTime() >= endDate.getTime()) {
            return wx.showToast({
                title: "开始时间不能大于结束时间",
                icon: "none"
            })
        }
        this.setData({
            startDate2: value
        })
        this.initHistoryData(2)

    },
    handleEndDateChange2(ev) {
        const {value} = ev.detail
        const endDate = new Date(value.replace(/-/g, '/'))
        const startDate = new Date(this.data.startDate2.replace(/-/g, '/'))
        if (endDate.getTime() <= startDate.getTime()) {
            return wx.showToast({
                title: "结束时间要大于开始时间",
                icon: "none"
            })
        }
        this.setData({
            endDate2: value
        })
        this.initHistoryData(2)
    },


    handleStartDateChange(ev) {
        const {value} = ev.detail
        const startDate = new Date(value.replace(/-/g, '/'))
        const endDate = new Date(this.data.endDate1)
        if (startDate.getTime() >= endDate.getTime()) {
            return wx.showToast({
                title: "开始时间不能大于结束时间",
                icon: "none"
            })
        }
        this.setData({
            startDate1: value
        })
        this.initHistoryData(1)
    },
    handleEndDateChange(ev) {
        const {value} = ev.detail
        const endDate = new Date(value.replace(/-/g, '/'))
        const startDate = new Date(this.data.startDate1.replace(/-/g, '/'))
        if (endDate.getTime() <= startDate.getTime()) {
            return wx.showToast({
                title: "结束时间要大于开始时间",
                icon: "none"
            })
        }
        this.setData({
            endDate1: value
        })
        this.initHistoryData(1)
    },
    async initData() {
        const {mobile} = this.data.userInfo
        const {active_id} = this.data
        const resp = await request({
            url: "/shujutongji/index",
            data: {
                store_id: active_id,
                mobile
            }
        })
        const {code, data} = resp
        if (code !== 1) return
        const {todaydingdan, todayshouru} = data
        this.setData({
            ding_dan: todaydingdan,
            shou_ru: todayshouru,
        })
    },
    onShow() {
        if (!this.data.token) {
            return wx.showModal({
                title: '提示',
                content: "请您登录后在来查看!",
                showCancel: true,
                success: ev => {
                    if (ev.cancel) return
                    wx.reLaunch({
                        url: "/pages/public/login"
                    })
                }
            })
        }
        this.initData()
        this.initHistoryData()
    },
    handleTest() {
    }
})
